package com.dianganjiao.lib.util;

import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;


import com.dianganjiao.lib.R;
import com.dianganjiao.lib.wiget.IconTextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 数据工具类
 *
 * @author Jidcoo
 */
public class ViewUtil {
    private static DateFormat dateFormat = new SimpleDateFormat("MM-dd");
    private static StringBuilder builder = new StringBuilder();

    /**
     * 获取评论/回复的时间差
     *
     * @param time
     * @return
     */
    public static String getTime(long time) {
        String timeStr = null;
        long nowTime = System.currentTimeMillis();
        int day = (int) ((nowTime - time) / 86400000);
        int hour = (int) ((nowTime - time) / 3600000);
        int minute = (int) ((nowTime - time) / 60000);
        int second = (int) ((nowTime - time) / 1000);
        if (day >= 365) {
            int c = day / 365;
            timeStr = c + "年前";
        } else if (day >= 1) {
            timeStr = dateFormat.format(time);
        } else if (hour >= 1) {
            timeStr = hour + "小时前";
        } else if (minute >= 1) {
            timeStr = minute + "分钟前";
        } else if (second > 0) {
            timeStr = second + "秒前";
        } else {
            timeStr = "刚刚";
        }
        return timeStr;
    }



    public static int dpToPx(int dps, Context context) {
        return Math.round(context.getResources().getDisplayMetrics().density * dps);
    }


    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public static View addTitle(View container){
        LinearLayout parent = new LinearLayout(container.getContext());
        parent.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        parent.setBackgroundColor(ContextCompat.getColor(container.getContext(),android.R.color.white));

        RelativeLayout titleLayout = new RelativeLayout(container.getContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DPUtil.dip2px(40));
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        titleLayout.setLayoutParams(params);
        parent.setOrientation(LinearLayout.VERTICAL);

        IconTextView iconTextView = new IconTextView(container.getContext());
        RelativeLayout.LayoutParams backParam = new RelativeLayout.LayoutParams(DPUtil.dip2px(40),DPUtil.dip2px(40));
        backParam.addRule(RelativeLayout.CENTER_VERTICAL);
        iconTextView.setLayoutParams(backParam);
        iconTextView.setText(container.getContext().getString(R.string.icon_more));
        iconTextView.setRotation(180);
        iconTextView.setTextSize(15);
        iconTextView.setGravity(Gravity.CENTER);
        iconTextView.setTextColor(ContextCompat.getColor(container.getContext(), android.R.color.white));
        iconTextView.setId(R.id.activity_back_layout_id);
        titleLayout.addView(iconTextView);

        IconTextView titleTextView = new IconTextView(container.getContext());
        RelativeLayout.LayoutParams titleParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        titleParam.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTextView.setLayoutParams(titleParam);
        titleTextView.setTextSize(16);
        titleTextView.setTextColor(ContextCompat.getColor(container.getContext(),android.R.color.white));
        titleTextView.setId(R.id.activity_title_id);
        titleLayout.addView(titleTextView);

        RelativeLayout topLayout = new RelativeLayout(container.getContext());
        topLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Screen.Companion.getStatusBarHeight(container.getContext())+DPUtil.dip2px(40)));
        topLayout.setBackgroundColor(ContextCompat.getColor(container.getContext(), R.color.app_main_color));
        topLayout.addView(titleLayout);

        parent.addView(topLayout);
        parent.addView(container,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        return parent;
    }
}
